package com.leetcode.Offer;

import java.util.ArrayList;
import java.util.List;

public class JZ34_2 {
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if (root == null) {
            return lists;
        }
        dfs(root, target, new ArrayList<>(), lists);

        return lists;
    }

    public void dfs(TreeNode root, int target, List<Integer> list, List<List<Integer>> lists) {
        int tmp = root.val;
        list.add(tmp);

        if (tmp == target && root.left == null && root.right == null) {
            lists.add(new ArrayList<>(list));
        }
        if (root.left != null) {
            dfs(root.left, target - tmp, list, lists);
        }
        if (root.right != null) {
            dfs(root.right, target - tmp, list, lists);
        }

        list.remove(list.size() - 1);

    }

}
